package model

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

var db *gorm.DB

type Model struct {}

type Return struct {
	code	int
	msg		string
}

type LastInsterId struct {
	Id int `json:"LAST_INSERT_ID"`
}

func init(){
	dbType := "mysql"
	dbName := "golang"
	user := "admin"
	password := "admin"
	host := "localhost:3306"
	tbFix := "go_"
	var err error
	db,err = gorm.Open(dbType,fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
		user,
		password,
		host,
		dbName))
	if err != nil{
		panic(err)
	}
	gorm.DefaultTableNameHandler = func (db *gorm.DB, defaultTableName string) string  {
		return tbFix + defaultTableName;
	}
	db.LogMode(true)
	db.SingularTable(true)
	db.DB().SetMaxIdleConns(10)
	db.DB().SetMaxOpenConns(100)
}

func CloseDB() {
	defer db.Close()
}